(ns tests
  (:use clojure.test)
  (:use readbinfile)
  (:use readerManager)
  (:use window)
)

(def testfile "db-1x2Tests.db")
(def vect {:magic 258, :offset 74, :num-fields 7, :fields [["name" 64] ["location" 64] ["size" 4] ["smoking" 1] ["rate" 8] ["date" 10] ["owner" 8]], :records [{:owner "",
           :date "2005/03/23", :rate "$210.00", :smoking "N", :size "2", :location "Smallville", :name "Excelsior", :deleted true}
           {:owner "", :date "2004/11/15", :rate "$150.00", :smoking "Y", :size "4", :location "Smallville", :name "Splendide",:deleted true}
           {:owner "", :date "2003/02/08", :rate "$170.00", :smoking "N", :size "4", :location "Whoville", :name "Excelsior", :deleted true}
           {:owner "", :date"2003/11/24", :rate "$180.00", :smoking "Y", :size "2", :location "Whoville", :name "Splendide", :deleted false}
           {:owner "", :date "2004/06/14", :rate "$160.00", :smoking "N", :size "4", :location "Whoville", :name "Pandemonium", :deleted false}
           {:owner "", :date "2003/07/26", :rate "$80.00", :smoking "N", :size "6", :location "Metropolis", :name "Dew Drop Inn", :deleted false}
           {:owner "", :date "2005/03/10", :rate "$240.00", :smoking "Y", :size "2", :location "Metropolis", :name "Splendide", :deleted false}
           {:owner "", :date "2005/02/01", :rate "$200.00", :smoking "Y", :size "2", :location "Pleasantville", :name "Splendide", :deleted false}
           {:owner "", :date "2004/11/04", :rate "$180.00", :smoking "N", :size "6", :location "Pleasantville", :name "Palace", :deleted false}
           {:owner "", :date "2003/11/21", :rate "$80.00", :smoking "Y", :size "2", :location "Pleasantville", :name "Excelsior", :deleted false}
           {:owner "", :date "2004/03/17", :rate "$240.00", :smoking "N", :size "4", :location "Digitopolis", :name "Splendide", :deleted false}
           {:owner "", :date "2005/09/16", :rate "$130.00", :smoking "N", :size "6", :location "Digitopolis", :name "Palace", :deleted false}
           {:owner "", :date "2003/06/27", :rate "$80.00", :smoking "N", :size "4", :location "Atlantis", :name "Grandview", :deleted false}
           {:owner "", :date "2003/10/05", :rate "$190.00", :smoking "N", :size "4", :location "Atlantis", :name "Bed & Breakfast & Business", :deleted false}
           {:owner "", :date "2005/08/19", :rate "$210.00", :smoking "Y", :size "6", :location "EmeraldCity", :name "Pandemonium", :deleted false}
           {:owner "", :date "2003/02/08", :rate "$100.00", :smoking "N", :size "6", :location "EmeraldCity", :name "Grandview", :deleted false}
           {:owner "", :date "2003/10/16", :rate "$180.00", :smoking "Y", :size "6", :location "EmeraldCity", :name "Splendide", :deleted false}
           {:owner "", :date "2003/05/17", :rate "$150.00", :smoking "Y", :size "6", :location "Bali Hai", :name "Excelsior", :deleted false}
           {:owner "", :date "2005/02/11", :rate "$140.00", :smoking "N", :size "6", :location "Bali Hai", :name "Bed & Breakfast & Business", :deleted false}
           {:owner "", :date "2003/01/19", :rate "$200.00", :smoking "Y", :size "4", :location "Xanadu", :name "Dew Drop Inn", :deleted false}
           {:owner "", :date "2004/11/07", :rate "$230.00", :smoking "N", :size "4", :location "Xanadu", :name "Castle", :deleted false}
           {:owner "", :date "2004/09/07", :rate "$220.00", :smoking "N", :size "4", :location "Paravel", :name "Excelsior", :deleted false}
           {:owner "", :date "2005/11/06", :rate "$110.00", :smoking "Y", :size "4", :location "Paravel", :name "Castle", :deleted false}
           {:owner "", :date "2004/12/17", :rate "$90.00", :smoking "N", :size "4", :location "Hobbiton", :name "Castle", :deleted false}
           {:owner "", :date "2004/12/23", :rate "$140.00", :smoking "N", :size "6", :location "Hobbiton", :name "Splendide", :deleted false}
           {:owner "", :date "2004/04/12", :rate "$80.00", :smoking "N", :size "6", :location "Hobbiton", :name "Excelsior", :deleted false}
           {:owner "", :date "2005/04/23", :rate "$110.00", :smoking "N", :size "4", :location "Lendmarch", :name "Grandview", :deleted false}
           {:owner "", :date "2003/05/19", :rate "$250.00", :smoking "Y", :size "4", :location "Lendmarch", :name "Splendide", :deleted false}
           {:owner "", :date "2003/04/20", :rate "$120.00", :smoking "Y", :size "4", :location "Lendmarch", :name "Palace", :deleted false}
           {:owner "Me", :date "2010/05/16", :rate "$100.00", :smoking "N", :size "1", :location "city", :name "hotel", :deleted true}]
          }
  )


(println "Automatic Tests")
;example
(deftest examples  
    (is (= 4 (+ 2 2)) "TRUE")
    (is (= 7 (+ 3 4)) "TRUE")
)
;end example

;------------------------------------------
;readbinfile tests
(deftest testread-bin-file
 (is (thrown? Exception (read-bin-file testfile))"NO EXCEPTION. Correct reading")
)

(deftest readbinfiletest
  (is (= vect (read-bin-file testfile)) "TRUE")
)

;end readbinfile tests

;------------------------------------------
;readerManager tests
(deftest getNumFieldstest
  (is (= 30 (get-numFields testfile)) "Not 30")
)

(deftest getFieldsNil
  (is (= nil (get-fields testfile))"Correct")
)

(def db2tests "db-1x2 Tests2.db")
(deftest testaddreg
 (is (thrown? Exception (new-values db2tests 30 "name" "city" "1" "T" "10.00" "date" "owner"))"NO EXCEPTION.Correct Writting")
)
;end readerManager tests

;------------------------------------------
;window tests
(deftest functions
  (is (= 1 (parse-int "1")))
  (is (= 2 (parse-int 2)))
  (is (= 1.0 (parse-float "1.0")))
  (is (= 2.0 (parse-float "2.0")))
)

(deftest getFieldsNil
  (is (= true (blank? " ")))
  (is (= true (blank? "")))
  (is (= true (blank? "hi")))
)

(is (= 8 tableSizeX))
(is (< 7 tableSizeX))
(is (<= 28 tableSizeY))

(is (thrown? Exception (get-records testfile))"NO EXCEPTION. Correct record's getting")

(def testrecords (get-records testfile))

(deftest testFilter
  (is (< 0 (count (filterT testrecords "Hobbiton" :location tableSizeX tableSizeY)) ))
  (is (> 0 (count (filterT testrecords "Nowhere Land" :location tableSizeX tableSizeY)) ))
  (is (> 0 (count (filterT testrecords "DN" :smoking tableSizeX tableSizeY)) ))
  (is (> 0 (count (filterT testrecords "Y" :smoking tableSizeX tableSizeY)) ))
  (is (< 0 (count (filterMoreRateThan testrecords "100.00" :rate tableSizeX tableSizeY)) ))
  (is (< 0 (count (filterMoreRateThan testrecords "400.00" :rate tableSizeX tableSizeY)) ))
  (is (< 0 (count (filterMaximum testrecords "100.00" :rate tableSizeX tableSizeY)) ))
  (is (< 0 (count (filterMaximum testrecords "1.00" :rate tableSizeX tableSizeY)) ))
  (is (< 0 (count (filterLessRateThan testrecords "1.00" :rate tableSizeX tableSizeY)) ))
  (is (< 0 (count (filterLessRateThan testrecords "100.00" :rate tableSizeX tableSizeY)) ))
  (is (< 0 (count (filterMoreThan testrecords "1.00" :rate tableSizeX tableSizeY)) ))
  (is (< 0 (count (filterMoreThan testrecords "500.00" :rate tableSizeX tableSizeY)) ))
  (is (< 0 (count (filterVec testrecords "Palace" :name tableSizeX tableSizeY)) ))  
) 
 
;end window tests

(run-tests)
(println (get-vals 1 8 testrecords))